package com.twotiger.and.activity.account.recharge;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.bean.Coupon;
import com.twotiger.and.bean.Order;
import com.twotiger.and.bean.RechargeBank;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.StringUtils;

/**
 * 充值layout
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月17日 上午11:41:43
 */
public class RechargeLayout {

    /**可用余额*/
    public TextView money_avaliable;

    /**投资充值信息*/
    public LinearLayout inverst_layout;
    public TextView inverst_amount;
    public TextView inverst_other;
    public EditText charge_amount_et;

    /**第一次以后充值*/
    public RelativeLayout second_layout;
    public ImageView bankcard_icon;
    public TextView bankcard_no;
    public TextView bankcard_name;
    public TextView bankcard_limit_info;

    /**第一次充值*/
    public LinearLayout first_layout;
    public TextView choice_bank;
    public EditText input_card_no;

    /**第一次充值提醒*/
    //    public View tips_layout;
    public Button iknow_btn;
    public TextView tips_text;
    /**确认按钮*/
    public Button charge_ok_button;

    private LinearLayout ll_isneedshow;

    private RelativeLayout rl_useotherbank;

    private TextView tv_charge_tip;

    private LinearLayout ll_charge_tip;

    private LinearLayout ll_top1, ll_top2, inverst_layout_2;

    private TextView money_avaliable_2, inverst_amount_2;

    private TextView tv_unbind_bank; //解绑

    private TextView tv_contact;

    public RechargeLayout(View view) {
        money_avaliable = (TextView) view.findViewById(R.id.money_avaliable);
        inverst_layout = (LinearLayout) view.findViewById(R.id.inverst_layout);
        inverst_amount = (TextView) view.findViewById(R.id.inverst_amount);
        inverst_other = (TextView) view.findViewById(R.id.inverst_other);
        charge_amount_et = (EditText) view.findViewById(R.id.charge_amount_et);
        second_layout = (RelativeLayout) view.findViewById(R.id.second_layout);
        bankcard_icon = (ImageView) view.findViewById(R.id.bankcard_icon);
        bankcard_no = (TextView) view.findViewById(R.id.bankcard_no);
        bankcard_name = (TextView) view.findViewById(R.id.bankcard_name);
        bankcard_limit_info = (TextView) view.findViewById(R.id.bankcard_limit_info);
        first_layout = (LinearLayout) view.findViewById(R.id.first_layout);
        choice_bank = (TextView) view.findViewById(R.id.choice_bank);
        input_card_no = (EditText) view.findViewById(R.id.input_card_no);
        //        tips_layout = view.findViewById(R.id.tips_layout);
        iknow_btn = (Button) view.findViewById(R.id.iknow_btn);
        tips_text = (TextView) view.findViewById(R.id.tips_text);
        charge_ok_button = (Button) view.findViewById(R.id.charge_ok_button);
        ll_isneedshow = (LinearLayout) view.findViewById(R.id.ll_isneedshow);
        rl_useotherbank = (RelativeLayout) view.findViewById(R.id.rl_useotherbank);
        tv_charge_tip = (TextView) view.findViewById(R.id.tv_charge_tip);
        ll_charge_tip = (LinearLayout) view.findViewById(R.id.ll_charge_tip);
        tv_unbind_bank = (TextView) view.findViewById(R.id.tv_unbind_bank);
        tv_contact = (TextView) view.findViewById(R.id.tv_contact);

        ll_top1 = (LinearLayout) view.findViewById(R.id.ll_top1);
        ll_top2 = (LinearLayout) view.findViewById(R.id.ll_top2); //活期充值顶部布局
        inverst_layout_2 = (LinearLayout) view.findViewById(R.id.inverst_layout_2); //活期充值顶部布局
        money_avaliable_2 = (TextView) view.findViewById(R.id.money_avaliable_2);
        inverst_amount_2 = (TextView) view.findViewById(R.id.inverst_amount_2);

        charge_amount_et.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0)
                    return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });
    }

    /**
     * 可用余额
     * @param avaliable
     */
    public void setAvaliable(double avaliable) {
        money_avaliable.setText(ArithUtils.coverMoneyComma(String.valueOf(avaliable)));
        money_avaliable_2.setText(ArithUtils.coverMoneyComma(String.valueOf(avaliable)));
    }

    public void setChargeAmt(double amt) {
        charge_amount_et.setText(ArithUtils.coverMoney(String.valueOf(amt)));
        inverst_amount.setText(ArithUtils.coverMoney(String.valueOf(amt)));
    }

    public void first(RechargeBank bank) {
        input_card_no.setVisibility(View.VISIBLE);
        //        tips_layout.setVisibility(View.GONE);
        first_layout.setVisibility(View.VISIBLE);
        second_layout.setVisibility(View.GONE);
        iknow_btn.setText("我知道了");
        tips_text.setText("您需要先绑定一张快捷充值卡\n每个用户只能绑定本人名下的一张银行卡，建议绑定您的常用卡");
        charge_amount_et.setHint("请输入充值金额(" + ArithUtils.coverMoney(bank.getMinAmt()) + "元起)");
    }

    public void nonFirst(RechargeBank bank) {
        input_card_no.setVisibility(View.GONE);
        //        tips_layout.setVisibility(View.GONE);
        first_layout.setVisibility(View.GONE);
        second_layout.setVisibility(View.VISIBLE);
        bankcard_no.setText(StringUtils.hidecardMidString(bank.cardNo));
        bankcard_name.setText(bank.name);
        charge_amount_et.setHint("单笔最高" + bank.getPerAmt() + "万");
        bankcard_limit_info.setText("限额:" + ArithUtils.coverMoney(bank.getPerAmt()) + "万元/笔;"
                                    + ArithUtils.coverMoney(bank.getDayAmt()) + "万元/天");
    }

    public void showOtherButton() {
        ll_isneedshow.setVisibility(View.VISIBLE);
    }

    /**
     * 是否显示解绑按钮
     * 
     * @param boo
     */
    public void isShowUnbindBtn(boolean boo) {
        if (boo) {
            tv_unbind_bank.setVisibility(View.VISIBLE);
        } else {
            tv_unbind_bank.setVisibility(View.INVISIBLE);
        }
    }

    public void showChargeUptip(String msg) {
        ll_charge_tip.setVisibility(View.VISIBLE);
        tv_charge_tip.setText(msg);
    }

    public void setBankIcon(Bitmap bit) {

        bankcard_icon.setBackgroundDrawable(new BitmapDrawable(bit));
    }

    /**
     * 活期充值
     */
    public void current(String amount, double avliable) {
        ll_top1.setVisibility(View.GONE);
        inverst_layout.setVisibility(View.GONE);
        ll_top2.setVisibility(View.VISIBLE);
        inverst_layout_2.setVisibility(View.VISIBLE);

        inverst_amount_2.setText(ArithUtils.coverMoneyComma(amount));
        charge_amount_et.setText(ArithUtils.coverMoney(ArithUtils.sub(Double.parseDouble(amount),
            avliable) + ""));
    }

    /**
     * 普通充值
     */
    public void common() {
        inverst_layout.setVisibility(View.GONE);
    }

    public void order(Order order, Coupon coupon) {
        inverst_layout.setVisibility(View.VISIBLE);
        double couponAmt = 0d;
        if (coupon == null) {
            inverst_other.setVisibility(View.GONE);
        } else {
            if (coupon.getAwardType() == 4) {//代金券
                inverst_other.setText("已经使用" + coupon.getAwardValue() + "元代金卷");
                inverst_other.setVisibility(View.VISIBLE);
                couponAmt = coupon.getAwardValue();
            } else {
                inverst_other.setVisibility(View.GONE);
            }
        }
        double temp = ArithUtils.sub(order.getInverstamount(), order.getCanuse());
        setChargeAmt(ArithUtils.sub(temp, couponAmt));

    }

    public void setOnclickListener(OnClickListener listener) {
        iknow_btn.setOnClickListener(listener);
        charge_ok_button.setOnClickListener(listener);
        rl_useotherbank.setOnClickListener(listener);
        choice_bank.setOnClickListener(listener);
        ll_charge_tip.setOnClickListener(listener);
        tv_unbind_bank.setOnClickListener(listener);
        tv_contact.setOnClickListener(listener);
    }

}
